package com.warm.mybatis.config;

import com.mybatisflex.core.FlexGlobalConfig;
import com.warm.mybatis.listener.HhInsertListener;
import com.warm.mybatis.listener.HhUpdateListener;
import com.warm.select.api.orm.domain.BaseEntity;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.dynamicds.DynamicDataSource;

import javax.sql.DataSource;

/**
 * MybatisFlex配置
 *
 * @author warm
 */
@Configuration
public class MybatisConfig {


    /**
     * 动态数据源（直接注入）
     *
     * @param dataSource 数据源
     * @return
     */
    @Bean(name = "db1" , typed = true)
    public DataSource dbUser(@Inject("${dataSource.db1}") DynamicDataSource dataSource) {
        return dataSource;
    }

    /**
     * MybatisFlex全局配置
     *
     * @param globalConfig 数据源
     */
    @Bean
    public void db1_cfg(@Db("db1") FlexGlobalConfig globalConfig) {

        // 注册全局填充监听器
        globalConfig.registerInsertListener(new HhInsertListener(), BaseEntity.class);
        globalConfig.registerUpdateListener(new HhUpdateListener(), BaseEntity.class);

    }
}
